/*
 * This file is a part of the SWFer project.
 *
 * Copyright (C) 2012 Michael Bradshaw <mjbshaw@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include "ByteReader.hpp"

namespace swfer
{

// TODO: check for out of bounds...
// TODO: when reading a structure, make sure its values haven't been set (i.e.
// clear the vector before adding to it when reading a packet)
ByteReader::ByteReader(std::istream& stream) : stream(stream),
    mask(0)
{
}

// Note: functions which actually read from the buffer and touch it
// (so far only those functions which read primitive types) need to
// account for the mask and skip a byte if necessary.
void ByteReader::read(int8_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 1);
}

void ByteReader::read(int16_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 2);
}

void ByteReader::read(int32_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 4);
}

void ByteReader::read(uint8_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 1);
}

void ByteReader::read(uint16_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 2);
}

void ByteReader::read(uint32_t& value)
{
    if (mask)
    {
		stream.ignore();
        mask = 0;
    }
	
	stream.read(reinterpret_cast<char*>(&value), 4);
}

void ByteReader::read(Rect& value)
{
    read(value.bits);
    value.xMin.bits = value.xMax.bits = value.yMin.bits = value.yMax.bits = value.bits;
    read(value.xMin);
    read(value.xMax);
    read(value.yMin);
    read(value.yMax);
	if (mask)
	{
		stream.ignore();
		mask = 0;
	}
}

void ByteReader::read(Fixed16& value)
{
    read(value.fraction);
    read(value.integer);
}

void ByteReader::read(Fixed32& value)
{
    read(value.fraction);
    read(value.integer);
}

void ByteReader::read(TagHeader& value)
{
    uint16_t tmp = 0;
    read(tmp);

    value.code = tmp >> 6;
    value.length = tmp & 0x3f;
    if (value.length == 0x3f)
    {
        read(value.length);
    }
}

void ByteReader::read(SwfHeader& value)
{
    read(value.signature[0]);
    read(value.signature[1]);
    read(value.signature[2]);

    // TODO: handle compression!
    if (value.signature[0] != 'F')
    {
        // It's compressed!
    }

    read(value.version);
    read(value.fileLength);
    read(value.frameSize);
    read(value.frameRate);
    read(value.frameCount);
}

void ByteReader::read(Packet& value)
{
	read(value.header);
	
	value.data.resize(value.header.length);
	if (value.header.length > 0)
	{
		stream.read(value.data.data(), value.header.length);
	}
}

void ByteReader::read(std::string& value)
{
	char c;
	while (stream.read(reinterpret_cast<char*>(&c), 1) && c)
	{
		value.push_back(c);
	}
}

void ByteReader::read(std::vector<char>& value)
{
	std::istream::streampos pos = stream.tellg();
	stream.seekg(0, std::ios::end);

	std::istream::streampos end = stream.tellg();
	stream.seekg(pos);

	value.resize(static_cast<unsigned int>(end - pos));
	stream.read(value.data(), value.size());
}

void ByteReader::skip(std::size_t bytes)
{
	stream.seekg(bytes, std::ios::cur);
}

}